<?php
namespace Utuo;

class UtuoLog {
	
	// 日志内容某一项内容最大长度
	const MAX_LENGTH = 200;
	
	/**
	 * 日志写入
	 * @param unknown $pid 合作商id
	 * @param unknown $cid 客户id
	 * @param unknown $uid 用户id
	 */
	public static function write($tableName,$pid,$cid,$aid,$uid,$userName) {
		$post = $_POST;
		foreach ( $post as $k => &$v ) {
			if ( is_string($v) ) {
				$v = iconv_substr($v, 0, self::MAX_LENGTH);
			}
		}
		$data = array(
				'pid' => $pid,
				'cid' => $cid,
				'aid' => $aid,
				'uid' => $uid,
				'userName' => $userName,
				'requestMethod' => IS_AJAX ? 'AJAX' : REQUEST_METHOD,
				'requestUri' => $_SERVER['REQUEST_URI'],
				'requestHost' => $_SERVER['HTTP_HOST'],
				'module' => MODULE_NAME,
				'controller' => CONTROLLER_NAME,
				'action' => ACTION_NAME,
				'get' => serialize($_GET),
				'post' => serialize($post),
				'session' => serialize($_SESSION[C('SESSION_PREFIX')]),
				'userAgent' => $_SERVER["HTTP_USER_AGENT"],
				'ip' => get_client_ip()
		);
	
		$m = M($tableName);
		$m->add($data);
	}
	
	public static function cut( $string ) {
		return iconv_substr($string, 0, self::MAX_LENGTH);
	}
	
// 	public static function doLogPost( $tableName, $session, $cfg = array() ) {
// 		$url = 'http://1.utuolog.sinaapp.com/index.php/Log/log';
// 		$data = array(
// 				'tableName' => $tableName,
// 				'ip' => get_client_ip(),
// 				'request_method' => $_SERVER['REQUEST_METHOD'],
// 				'script_url' => $_SERVER['SCRIPT_URL'],
// 				'user_agent' => $_SERVER['HTTP_USER_AGENT'],
// 				'controller' => CONTROLLER_NAME,
// 				'action' => ACTION_NAME,
// 				'get' => serialize($_GET),
// 				'post' => serialize($_POST),
// 				'session' => serialize($session),
// 				'create_time' => time()
// 		);
// 		$data = array_merge($data, $cfg);
// 		$result = self::post($url, $data);
// 	}
	  
// 	static function post($url, $data = null) {
// 		$ch = curl_init() ;
// 		curl_setopt($ch, CURLOPT_HEADER, 0);
// 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上
// 		curl_setopt($ch, CURLOPT_URL, $url);
// 		curl_setopt($ch, CURLOPT_POST, 1);
// 		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// 		$return = curl_exec($ch);
// 		curl_close($ch);
// 		return $return;
// 	}
	
// 	static function get($url) {
// 		$ch = curl_init() ;
// 		curl_setopt($ch, CURLOPT_HEADER, 0);
// 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上
// 		curl_setopt($ch, CURLOPT_URL, $url);
// 		$return = curl_exec($ch);
// 		curl_close($ch);
// 		return $return;
// 	}
	
}